package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.EmployeeShiftBo;
import org.eiahe.hr.attendance.domain.vo.EmployeeShiftVo;
import org.eiahe.hr.attendance.domain.vo.ShiftVo;
import org.eiahe.hr.attendance.service.IEmployeeShiftService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * 员工班次管理控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/employeeShift")
public class EmployeeShiftController extends BaseController {

    private final IEmployeeShiftService employeeShiftService;

    /**
     * 查询员工班次关联列表
     */
    @SaCheckPermission("attendance:employeeShift:list")
    @GetMapping("/list")
    public TableDataInfo<EmployeeShiftVo> list(EmployeeShiftBo bo, PageQuery pageQuery) {
        return employeeShiftService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取员工班次关联详细信息
     */
    @SaCheckPermission("attendance:employeeShift:query")
    @GetMapping("/{id}")
    public R<EmployeeShiftVo> getInfo(@PathVariable Long id) {
        return R.ok(employeeShiftService.queryById(id));
    }

    /**
     * 新增员工班次关联
     */
    @SaCheckPermission("attendance:employeeShift:add")
    @Log(title = "员工班次管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EmployeeShiftBo bo) {
        return toAjax(employeeShiftService.insertByBo(bo));
    }

    /**
     * 修改员工班次关联
     */
    @SaCheckPermission("attendance:employeeShift:edit")
    @Log(title = "员工班次管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmployeeShiftBo bo) {
        return toAjax(employeeShiftService.updateByBo(bo));
    }

    /**
     * 删除员工班次关联
     */
    @SaCheckPermission("attendance:employeeShift:remove")
    @Log(title = "员工班次管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(employeeShiftService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 批量分配班次给员工
     */
    @SaCheckPermission("attendance:employeeShift:batchAssign")
    @Log(title = "批量分配班次", businessType = BusinessType.INSERT)
    @PostMapping("/batchAssign")
    public R<Void> batchAssign(@RequestBody BatchAssignShiftBo bo) {
        return toAjax(employeeShiftService.batchAssignShift(
                bo.getEmployeeNos(),
                bo.getShiftId(),
                bo.getEffectiveDate(),
                bo.getExpiryDate()));
    }

    /**
     * 查询员工当前生效的班次
     */
    @SaCheckPermission("attendance:employeeShift:query")
    @GetMapping("/current/{employeeNo}")
    public R<ShiftVo> getCurrentShift(@PathVariable String employeeNo,
            @RequestParam(required = false) LocalDate date) {
        return R.ok(employeeShiftService.queryEmployeeCurrentShift(employeeNo, date));
    }

    /**
     * 导出员工班次列表
     */
    @SaCheckPermission("attendance:employeeShift:export")
    @Log(title = "员工班次管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmployeeShiftBo bo, HttpServletResponse response) {
        List<EmployeeShiftVo> list = employeeShiftService.queryList(bo);
        ExcelUtil.exportExcel(list, "员工班次数据", EmployeeShiftVo.class, response);
    }

    /**
     * 批量分配班次业务对象
     */
    @lombok.Data
    public static class BatchAssignShiftBo {
        private List<String> employeeNos;
        private Long shiftId;
        private LocalDate effectiveDate;
        private LocalDate expiryDate;
    }
}
